package com.zjzywl.iotSendthings.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zjzywl.iotSendthings.entity.JDWPBGXX;
import com.zjzywl.iotSendthings.repository.InsertJDWPBGXXRepository;
import com.zjzywl.iotSendthings.service.UpdataInformationService;
import com.zjzywl.iotSendthings.util.Item;
import com.zjzywl.iotSendthings.util.WebResult;

@Controller
@RequestMapping(value = "/updata")
public class UpdataInformationController {
    @Autowired
    private InsertJDWPBGXXRepository insertRepository;
    @Autowired
    private UpdataInformationService updataInformationService;
    // 看守所编号
    @Value("${app.kssbh}")
    private String kssbh;

    /*
     * 修改送物数量
     */
    
    @RequestMapping(value = "/information")
    @ResponseBody
    public WebResult getInformation(Item item, HttpServletRequest request, HttpServletResponse response,
            HttpSession session) {
        List<String> sls = item.getSl();
        List<String> wps = item.getWp();
        String uuid = item.getUuid();
        // 得到session对象
        String jsr = (String) session.getAttribute("username");
        try {
            updataInformationService.getInformation(item, sls, wps, uuid, jsr);
            return WebResult.OK();
        } catch (Exception e) {
            return WebResult.FAIL("表单提交失败");
        }

    }

    @RequestMapping(value = "/del")
    @ResponseBody
    public WebResult delete(@PathParam("uuid") String uuid) {
        List<JDWPBGXX> bgxx = insertRepository.findByuuidList(uuid);
        for (int i = 0; i < bgxx.size(); i++) {
            if (bgxx != null) {
                bgxx.get(i).setBz("sc");
                insertRepository.save(bgxx.get(i));
            } else {
                return WebResult.FAIL("找不到该送物信息");
            }
        }
        return WebResult.OK();
    }
}